package edu.uic.kott.db;

import java.util.ArrayList;
import java.util.List;

import edu.uic.kott.model.ClassActivity;
import edu.uic.kott.model.Course;
import edu.uic.kott.model.Instructor;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class KottDataSource {

	private static final String LOGTAG = "KOTT";

	SQLiteOpenHelper dbHelper;
	SQLiteDatabase database;

	private static final String[] instructorTaIdNameColumns = {
		KottDBOpenHelper.INSTRUCTOR_ID,
		KottDBOpenHelper.INSTRUCTOR_NAME
	};

	private static final String[] instructorTaAllColumns = {
		KottDBOpenHelper.INSTRUCTOR_ID,
		KottDBOpenHelper.INSTRUCTOR_TYPE,
		KottDBOpenHelper.INSTRUCTOR_NAME,
		KottDBOpenHelper.EMAIL_ADD,
		KottDBOpenHelper.PHONE_NO,
		KottDBOpenHelper.OFFICE_LOCATION
	};

	private static final String[] classIdNameColumns = {
		KottDBOpenHelper.COURSE_ID,
		KottDBOpenHelper.COURSE_NAME
	};

	private static final String[] classAllColumns = {		
		KottDBOpenHelper.COURSE_NAME,
		KottDBOpenHelper.START_DATE,
		KottDBOpenHelper.END_DATE,
		KottDBOpenHelper.CLASS_DAYS,
		KottDBOpenHelper.START_TIME,
		KottDBOpenHelper.END_TIME,
		KottDBOpenHelper.TWSS,
		KottDBOpenHelper.CLASS_LOCATION,
		KottDBOpenHelper.INSTRUCTOR,
		KottDBOpenHelper.TA
	};

	private static final String[] activityIdNameColumns = {
		KottDBOpenHelper.ACTIVITY_ID,
		KottDBOpenHelper.ACTIVITY_NAME	
	};

	private static final String[] activityallColumns = {
		KottDBOpenHelper.ACTIVITY_NAME,
		KottDBOpenHelper.ACTIVITY_TYPE,
		KottDBOpenHelper.DUE_DATE,
		KottDBOpenHelper.DESCRIPTION,
		KottDBOpenHelper.MAX_GRADE,
		KottDBOpenHelper.IS_COMPLETED,
		KottDBOpenHelper.OBT_GRADE,
		KottDBOpenHelper.COURSE_ID

	};

	public KottDataSource(Context context){
		dbHelper = new KottDBOpenHelper(context);
		database = dbHelper.getWritableDatabase();
	}

	public void open(){
		database = dbHelper.getWritableDatabase();
		//Log.i(LOGTAG, "Database Opened");
	}

	public void close(){
		database.close();
		//Log.i(LOGTAG, "Database Closed");
	}

	//Insert query to add Instructors to database
	public long createInstructor(Instructor instructor){
		open();
		ContentValues values = new ContentValues();
		values.put(KottDBOpenHelper.INSTRUCTOR_TYPE, instructor.getInstructorType());
		values.put(KottDBOpenHelper.INSTRUCTOR_NAME, instructor.getInstructorName());
		values.put(KottDBOpenHelper.PHONE_NO, instructor.getPhoneNo());
		values.put(KottDBOpenHelper.EMAIL_ADD, instructor.getEmailAdd());
		values.put(KottDBOpenHelper.OFFICE_LOCATION, instructor.getOfficeLocation());

		long insertId = database.insert(KottDBOpenHelper.TABLE_INSTRUCTOR, null, values);
		instructor.setInstructorId(insertId);
		close();
		return instructor.getInstructorId();
	}

	//To populate the Instructor and Ta dropdown list lists on Add Edit Course Screen
	public List<Instructor> findAllByType(String type){
		open();
		List<Instructor> instructorTaList = new ArrayList<Instructor>();
		Instructor instructor = new Instructor();

		String where = KottDBOpenHelper.INSTRUCTOR_TYPE + " = ?"; 
		String[] whereArgs = null;

		if("Instructor" == type){
			whereArgs = new String [] {"Instructor"};
			instructor.setInstructorName(" - Select Instructor -");
		}
		else if ("Ta" == type){
			whereArgs = new String [] {"Teaching Assistant"};
			instructor.setInstructorName(" - Select TA -");
		}

		instructor.setInstructorId(0);
		instructorTaList.add(instructor);	//first value in drop down list will be Select Instructor/Ta

		Cursor cursor = database.query(KottDBOpenHelper.TABLE_INSTRUCTOR, instructorTaIdNameColumns, 
				where, whereArgs, null, null, null);

		if (cursor.getCount() > 0) {
			while(cursor.moveToNext()){
				instructor = new Instructor();
				instructor.setInstructorId(cursor.getLong(cursor.getColumnIndex(KottDBOpenHelper.INSTRUCTOR_ID)));
				instructor.setInstructorName(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.INSTRUCTOR_NAME)));
				instructorTaList.add(instructor);
			}			
		}
		close();
		cursor.close();
		return instructorTaList;
	}

	//Returns a list of Instructors and Ta's to populate the View All Instructors Screen
	public List<Instructor> findAllInstructorsTas(){
		open();
		List<Instructor> allInstructorsTaList = new ArrayList<Instructor>(); 

		Cursor cursor = database.query(KottDBOpenHelper.TABLE_INSTRUCTOR, instructorTaIdNameColumns, 
				null, null, null, null, null);

		if (cursor.getCount() > 0) {
			while(cursor.moveToNext()){
				Instructor ta = new Instructor();
				ta.setInstructorId(cursor.getLong(cursor.getColumnIndex(KottDBOpenHelper.INSTRUCTOR_ID)));
				Log.i(LOGTAG, "Instructor id retrived is: " + cursor.getLong(cursor.getColumnIndex(KottDBOpenHelper.INSTRUCTOR_ID)));
				ta.setInstructorName(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.INSTRUCTOR_NAME)));
				allInstructorsTaList.add(ta);
			}			
		}	
		close();
		cursor.close();
		return allInstructorsTaList;
	}

	//This method takes the instructor or Ta Id as argument and returns the respective details 
	public Instructor getInstructorTaDetails(String Id){
		open();
		Log.i(LOGTAG,"inside getInstructorTaDetails() with id " + Id);
		Instructor instructor = new Instructor();
		String whereClause = KottDBOpenHelper.INSTRUCTOR_ID + " = ?";
		String[] whereArgs = new String [] {Id};

		Cursor cursor = database.query(KottDBOpenHelper.TABLE_INSTRUCTOR, instructorTaAllColumns, 
				whereClause, whereArgs, null, null, null);

		if (cursor.getCount() > 0) {
			while(cursor.moveToNext()){				
				instructor.setInstructorId(cursor.getLong(cursor.getColumnIndex(KottDBOpenHelper.INSTRUCTOR_ID)));
				instructor.setInstructorType(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.INSTRUCTOR_TYPE)));
				instructor.setInstructorName(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.INSTRUCTOR_NAME)));
				instructor.setPhoneNo(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.PHONE_NO)));
				instructor.setEmailAdd(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.EMAIL_ADD)));
				instructor.setOfficeLocation(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.OFFICE_LOCATION)));				
			}			
		}
		close();
		cursor.close();
		return instructor;
	}

	//this function takes the instructor or Ta id and returns Name
	public String getInstructorTaName(String Id){
		open();
		Instructor instructor = new Instructor();

		String[] columnName = new String [] {KottDBOpenHelper.INSTRUCTOR_NAME};
		String whereClause = KottDBOpenHelper.INSTRUCTOR_ID + " = ?";
		String[] whereArgs = new String [] {Id};		

		Cursor cursor = database.query(KottDBOpenHelper.TABLE_INSTRUCTOR, columnName, 
				whereClause, whereArgs, null, null, null);

		if (cursor.getCount() > 0)
			while(cursor.moveToNext())				
				instructor.setInstructorName(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.INSTRUCTOR_NAME)));

		String name = "Not Available";

		close();
		cursor.close();
		
		if(instructor.getInstructorName() == null)		
			return name;
		else return instructor.getInstructorName();
	}

	//Update query to modify Instructor or TA details
	public void updateInstructor(Instructor instructor){
		open();
		ContentValues values = new ContentValues();
		values.put(KottDBOpenHelper.INSTRUCTOR_TYPE, instructor.getInstructorType());
		values.put(KottDBOpenHelper.INSTRUCTOR_NAME, instructor.getInstructorName());
		values.put(KottDBOpenHelper.PHONE_NO, instructor.getPhoneNo());
		values.put(KottDBOpenHelper.EMAIL_ADD, instructor.getEmailAdd());
		values.put(KottDBOpenHelper.OFFICE_LOCATION, instructor.getOfficeLocation());

		String whereClause = KottDBOpenHelper.INSTRUCTOR_ID + " = ?";
		String[] whereArgs = new String [] {String.valueOf(instructor.getInstructorId())};

		database.update(KottDBOpenHelper.TABLE_INSTRUCTOR, values, whereClause, whereArgs);
		close();
	}


	//method to delete instructor
	public boolean deleteInstructorTa(String id){
		open();
		//Delete from instructor table
		String where = KottDBOpenHelper.INSTRUCTOR_ID + "=" + id;
		int result = database.delete(KottDBOpenHelper.TABLE_INSTRUCTOR,where,null);


		//Also update any class table that has this instructor
		String updateInstructor = "update all_courses set instructor = '0' where instructor = 'id'"; 
		String updateTa = "update all_courses set ta = '0' where ta = 'id'";

		database.rawQuery(updateInstructor, null);
		database.rawQuery(updateTa, null);
		close();		

		return (result == 1);
	}

	public long createCourse(Course course){
		open();
		ContentValues values = new ContentValues();
		values.put(KottDBOpenHelper.COURSE_NAME, course.getCourseName());
		values.put(KottDBOpenHelper.START_DATE, course.getStartDate());
		values.put(KottDBOpenHelper.END_DATE, course.getEndDate());
		values.put(KottDBOpenHelper.CLASS_DAYS, course.getClassDays());
		values.put(KottDBOpenHelper.START_TIME, course.getStartTime());
		values.put(KottDBOpenHelper.END_TIME, course.getEndTime());
		values.put(KottDBOpenHelper.TWSS, course.getTargetedWSS());		
		values.put(KottDBOpenHelper.CLASS_LOCATION, course.getClassLocation());
		values.put(KottDBOpenHelper.INSTRUCTOR, course.getInstructor());
		values.put(KottDBOpenHelper.TA, course.getTa());

		long insertId =  database.insert(KottDBOpenHelper.TABLE_ALL_COURSES, null, values);
		course.setCourseId(insertId);

		close();
		return course.getCourseId();
	}

	//Returns a list of courses to populate the View All Instructors Screen
	public List<Course> findAllCourses(String activityScreen){
		open();
		List<Course> allCourses = new ArrayList<Course>();
		Course course = new Course();

		//if this function is called from AddEditActivity put first item as Select One
		if("AddEditActivity".equalsIgnoreCase(activityScreen)){
			course.setCourseId(0);
			course.setCourseName(" - Select One -");
			allCourses.add(course);
		}		

		Cursor cursor = database.query(KottDBOpenHelper.TABLE_ALL_COURSES, classIdNameColumns, 
				null, null, null, null, null);

		if (cursor.getCount() > 0) {
			while(cursor.moveToNext()){
				course = new Course();
				course.setCourseId(cursor.getLong(cursor.getColumnIndex(KottDBOpenHelper.COURSE_ID)));
				course.setCourseName(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.COURSE_NAME)));
				Log.i(LOGTAG, "Course id retrived is: " + course.getCourseId());
				allCourses.add(course);
			}			
		}
		close();
		cursor.close();
		return allCourses;
	}


	//This function queries course table with course id and 
	//calls another function which queries instructor table for instructor and ta names 
	public Course getCourseDetails(String id){
		open();
		Log.i(LOGTAG,"inside getCourseDetails() with id " + id); 
		Course course = new Course();

		String whereClause = KottDBOpenHelper.COURSE_ID + " = ?";
		String[] whereArgs = new String [] {id};

		Cursor cursor = database.query(KottDBOpenHelper.TABLE_ALL_COURSES, classAllColumns, 
				whereClause, whereArgs, null, null, null);

		Log.i(LOGTAG, "Returned " + cursor.getCount() + " all_course rows");

		if (cursor.getCount() > 0){
			while(cursor.moveToNext()){
				course.setCourseName(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.COURSE_NAME)));
				course.setClassDays(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.CLASS_DAYS)));
				course.setStartDate(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.START_DATE)));
				course.setEndDate(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.END_DATE)));
				course.setStartTime(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.START_TIME)));
				course.setEndTime(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.END_TIME)));
				course.setTargetedWSS(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.TWSS)));
				course.setClassLocation(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.CLASS_LOCATION)));
				course.setInstructor(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.INSTRUCTOR)));
				course.setTa(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.TA)));
				close();
			}

			Log.i(LOGTAG,"Instructor id is " + course.getInstructor());
			Log.i(LOGTAG,"TA id is " + course.getTa());

			Log.i(LOGTAG, "Instructor name is : " + getInstructorTaName(course.getInstructor()));
			Log.i(LOGTAG, "TA name is : " + getInstructorTaName(course.getTa()));

			course.setInstructor(getInstructorTaName(course.getInstructor())); 
			course.setTa(getInstructorTaName(course.getTa()));
		}
		cursor.close();
		return course;
	}

	//Update query to modify Course details
	public void updateCourse(Course course){
		open();
		ContentValues values = new ContentValues();
		values.put(KottDBOpenHelper.COURSE_NAME, course.getCourseName());
		values.put(KottDBOpenHelper.START_DATE, course.getStartDate());
		values.put(KottDBOpenHelper.END_DATE, course.getEndDate());
		values.put(KottDBOpenHelper.CLASS_DAYS, course.getClassDays());
		values.put(KottDBOpenHelper.START_TIME, course.getStartTime());
		values.put(KottDBOpenHelper.END_TIME, course.getEndTime());
		values.put(KottDBOpenHelper.TWSS, course.getTargetedWSS());		
		values.put(KottDBOpenHelper.CLASS_LOCATION, course.getClassLocation());
		values.put(KottDBOpenHelper.INSTRUCTOR, course.getInstructor());
		values.put(KottDBOpenHelper.TA, course.getTa());

		String whereClause = KottDBOpenHelper.COURSE_ID + " = ?";
		String[] whereArgs = new String [] {String.valueOf(course.getCourseId())};

		database.update(KottDBOpenHelper.TABLE_ALL_COURSES, values, whereClause, whereArgs);
		close();
	}

	//method to delete course
	public boolean deleteCourse(String id){
		open();
		//Delete from course table
		String where = KottDBOpenHelper.COURSE_ID + "=" + id;
		int result = database.delete(KottDBOpenHelper.TABLE_ALL_COURSES,where,null);
		close();

		//delete all activities related to the course being deleted
		deleteActivity(id, "course");

		return (result == 1);
	}

	//this function takes the course id and returns Name
	public String getCourseName(String id){
		open();
		Course course = new Course();

		String[] columnName = new String [] {KottDBOpenHelper.COURSE_NAME};
		String whereClause = KottDBOpenHelper.COURSE_ID + " = ?";
		String[] whereArgs = new String [] {id};		

		Cursor cursor = database.query(KottDBOpenHelper.TABLE_ALL_COURSES, columnName, 
				whereClause, whereArgs, null, null, null);

		if (cursor.getCount() > 0)
			while(cursor.moveToNext())				
				course.setCourseName((cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.COURSE_NAME))));

		close();
		cursor.close();
		return course.getCourseName();
	}

	//Returns a list of activities to populate the View All Activities Screen
	public List<ClassActivity> findAllActivities(String sort){
		open();
		List<ClassActivity> allActivities = new ArrayList<ClassActivity>();

		if("date".equalsIgnoreCase(sort))
			sort = KottDBOpenHelper.DUE_DATE;

		if("class".equalsIgnoreCase(sort))
			sort = KottDBOpenHelper.COURSE_ID;

		Cursor cursor = database.query(KottDBOpenHelper.TABLE_ACTIVITIES, activityIdNameColumns, 
				null, null, null, null, sort);

		if (cursor.getCount() > 0) {
			while(cursor.moveToNext()){
				ClassActivity acvty = new ClassActivity();
				acvty.setActivityId(cursor.getLong(cursor.getColumnIndex(KottDBOpenHelper.ACTIVITY_ID)));
				acvty.setActivityName(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.ACTIVITY_NAME)));

				Log.i(LOGTAG, "Course id retrived is: " + acvty.getActivityId());
				allActivities.add(acvty);
			}			
		}
		close();
		cursor.close();
		return allActivities;
	}

	//Add or Update Activity
	public long addUpdateActivity(ClassActivity acvty){
		Log.i(LOGTAG, "inside addUpdate Activity()");
		Log.i(LOGTAG, KottDBOpenHelper.CREATE_TABLE_ACTIVITIES);		
		open();
		ContentValues values = new ContentValues();
		values.put(KottDBOpenHelper.ACTIVITY_NAME, acvty.getActivityName());
		values.put(KottDBOpenHelper.ACTIVITY_TYPE, acvty.getActivityType());
		values.put(KottDBOpenHelper.DUE_DATE, acvty.getDueDate());
		values.put(KottDBOpenHelper.DESCRIPTION, acvty.getActivityDescription());
		values.put(KottDBOpenHelper.IS_COMPLETED, acvty.isCompleted());
		values.put(KottDBOpenHelper.MAX_GRADE, acvty.getMaxGrade());
		values.put(KottDBOpenHelper.OBT_GRADE, acvty.getObtGrade());		
		values.put(KottDBOpenHelper.COURSE_ID, acvty.getCourse());

		//add query
		if(acvty.getActivityId() == 0){
			long insertId =  database.insert(KottDBOpenHelper.TABLE_ACTIVITIES, null, values);
			acvty.setActivityId(insertId);
		}
		//update query
		/*else
		{
			String whereClause = KottDBOpenHelper.ACTIVITY_ID + " = ?";
			String[] whereArgs = new String [] {String.valueOf(acvty.getActivityId())};

			database.update(KottDBOpenHelper.TABLE_ACTIVITIES, values, whereClause, whereArgs);		
		}*/

		close();
		return acvty.getActivityId();
	}


	//This function queries activity table with activity id and 
	//calls another function which queries course table for course names 
	public ClassActivity getActivityDetails(String id){
		open();
		Log.i(LOGTAG,"inside getActivityDetails() with id " + id); 
		ClassActivity acvty = new ClassActivity();

		String whereClause = KottDBOpenHelper.ACTIVITY_ID + " = ?";
		String[] whereArgs = new String [] {id};

		Cursor cursor = database.query(KottDBOpenHelper.TABLE_ACTIVITIES, activityallColumns, 
				whereClause, whereArgs, null, null, null);

		Log.i(LOGTAG, "Returned " + cursor.getCount() + " activities rows");

		if (cursor.getCount() > 0){
			while(cursor.moveToNext()){
				acvty.setActivityType(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.ACTIVITY_TYPE)));
				acvty.setActivityName(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.ACTIVITY_NAME)));
				acvty.setDueDate(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.DUE_DATE)));
				acvty.setCourse(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.COURSE_ID)));
				acvty.setActivityDescription(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.DESCRIPTION)));
				acvty.setMaxGrade(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.MAX_GRADE)));
				acvty.setObtGrade(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.OBT_GRADE)));
				acvty.setCompleted(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.IS_COMPLETED)));
				close();
			}

			acvty.setCourse(getCourseName(acvty.getCourse()));

		}
		cursor.close();
		return acvty;
	}

	//method to delete activity
	public boolean deleteActivity(String id, String type){
		open();
		String where = "";
		if("activity".equalsIgnoreCase(type))
			where = KottDBOpenHelper.ACTIVITY_ID + "=" + id;

		//delete all activities of a particular course, if the course is deleted
		if("course".equalsIgnoreCase(type))			
			where = KottDBOpenHelper.COURSE_ID + "=" + id;

		int result = database.delete(KottDBOpenHelper.TABLE_ACTIVITIES,where,null);
		close();

		return (result == 1);
	}

	//addDates query to add course dates
	public void addDates(String courseId, List<String> dateList){
		open();
		for(int i=0; i<dateList.size();i++){
			ContentValues values = new ContentValues();
			values.put(KottDBOpenHelper.DATE,dateList.get(i));
			values.put(KottDBOpenHelper.COURSE_ID,courseId);
			values.put(KottDBOpenHelper.IS_ATTENDED,0);
			values.put(KottDBOpenHelper.IS_CANCELED,0);
			database.insert(KottDBOpenHelper.TABLE_ATTENDANCE, null, values);				
		}
	}

	//addDates query to add course dates
	public void updateDates(String courseId, List<String> dateList){
		open();
		for(int i=0; i<dateList.size();i++){
			ContentValues values = new ContentValues();
			values.put(KottDBOpenHelper.DATE,dateList.get(i));
			values.put(KottDBOpenHelper.COURSE_ID,courseId);
			values.put(KottDBOpenHelper.IS_ATTENDED,0);
			values.put(KottDBOpenHelper.IS_CANCELED,0);

			String whereClause = KottDBOpenHelper.COURSE_ID + " = ?";
			String[] whereArgs = new String [] {courseId};

			database.update(KottDBOpenHelper.TABLE_ATTENDANCE, values, whereClause, whereArgs);		
		}
	}	

	public int removeDates(String courseId){
		open();
		String whereClause = KottDBOpenHelper.COURSE_ID + " = ?";
		String[] whereArgs = new String [] {courseId};

		int rowsDeleted = database.update(KottDBOpenHelper.TABLE_ATTENDANCE, null, whereClause, whereArgs);
		close();

		return rowsDeleted;
	}


	//return all dates on which class was attended
	public List<String> datesAttended(String courseId){
		open();
		List<String> datesAttended = new ArrayList<String>(); 

		String query = "SELECT date FROM attendance WHERE course_id = "+ courseId + " AND is_attended = 1;";

		Log.i(LOGTAG, "Query " + query);
		Cursor cursor = database.rawQuery(query, null);
		Log.i(LOGTAG, "Returned " + cursor.getCount() + " attended rows");

		if (cursor.getCount() > 0){
			while(cursor.moveToNext()){
				datesAttended.add(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.DATE)));				
			}
			close();		

		}
		cursor.close();
		return datesAttended; 
	}

	//return all dates on which class is/was held
	public List<String> allClassDates(String courseId){
		open();
		List<String> datesAttended = new ArrayList<String>(); 

		String query = "SELECT date FROM attendance WHERE course_id = "+ courseId +";";

		Log.i(LOGTAG, "Query " + query);
		Cursor cursor = database.rawQuery(query, null);
		Log.i(LOGTAG, "Returned " + cursor.getCount() + " class date rows");

		if (cursor.getCount() > 0){
			while(cursor.moveToNext()){
				datesAttended.add(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.DATE)));				
			}
			cursor.close();
			close();	
		}
		return datesAttended; 
	}


	//function to mark class attendance
	public void markAttended(String courseId, String dateAttended){
		open();
		/*String whereClause = KottDBOpenHelper.COURSE_ID + " = ?" + ", " + KottDBOpenHelper.DATE + " = ?";
		String[] whereArgs = new String [] {courseId,dateAttended};
		ContentValues values = new ContentValues();
		values.put(KottDBOpenHelper.IS_ATTENDED,1);*/

		//String query = "UPDATE date FROM attendance WHERE course_id = "+ courseId + " AND date = " + dateAttended;
		String query = "UPDATE attendance SET is_attended = 1 WHERE course_id = " + courseId + " AND date = '" + dateAttended +"';";

		Log.i(LOGTAG, "Query to mark attended: " + query);
		Cursor cursor = database.rawQuery(query, null);
		Log.i(LOGTAG, "Marked attended! Rows affected " + String.valueOf(cursor.getCount()));

		//database.update(KottDBOpenHelper.TABLE_ATTENDANCE, values, whereClause, whereArgs);
		close(); 
	}

	//function to mark class canceled
	public void markCanceled(String courseId, String dateCanceled){
		open();

		String query = "UPDATE attendance SET is_attended = 0, is_canceled = 1 WHERE course_id = " + courseId + " AND date = '" + dateCanceled +"';";
		Log.i(LOGTAG, "Query to mark cancel: " + query);

		Cursor cursor = database.rawQuery(query, null);
		Log.i(LOGTAG, "marked canceled! Rows affected " + String.valueOf(cursor.getCount()));
		close(); 
	}	

	//insert query to add study sessions dates
	public void insertStudySessions(String courseId, String date, int count){
		open();

		ContentValues values = new ContentValues();
		values.put(KottDBOpenHelper.COURSE_ID, courseId);
		values.put(KottDBOpenHelper.DATE, date);
		values.put(KottDBOpenHelper.COUNT, count);
		database.insert(KottDBOpenHelper.TABLE_STUDY_SESSIONS, null, values);

		close();
	}

	public String retreiveStudySessions(String courseId, String date){
		open();
		String query = "SELECT count FROM study_sessions WHERE course_id = " + courseId + " AND date = '" + date + "';";
		Log.i(LOGTAG, "Query for retriving Study Session : " + query);
		String count = "0";
		Cursor cursor = database.rawQuery(query, null); 

		if (cursor.getCount() > 0){
			while(cursor.moveToNext()){
				count =	cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.COUNT));
			}	
			close();
		}
		cursor.close();
		return count;
	}
	
	public List<Integer> retreiveTotalTermStudySessions(String courseId){
		open();
		String query = "SELECT count FROM study_sessions WHERE course_id = " + courseId + ";";
		Log.i(LOGTAG, "Query for retriving Study Session : " + query);
		List<Integer> counts = new ArrayList<Integer>();
		counts.add(0);
		String count;
		Integer countInt;
		Cursor cursor = database.rawQuery(query, null); 

		if (cursor.getCount() > 0){
			while(cursor.moveToNext()){
				count =	cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.COUNT));
				countInt = Integer.parseInt(count);
				counts.add(countInt);
			}	
			close();
		}
		cursor.close();
		return counts;
	}

	//function to mark class attendance
	public void markActivityComplete(String activityId){
		open();

		String query = "UPDATE activities SET is_completed = 'true' WHERE activity_id = '" + activityId +"';";

		Log.i(LOGTAG, "Query to mark attended: " + query);
		database.rawQuery(query, null);
		close(); 
	}

	public String getActivityStatus(String activityId){
		open();
		String status = "false";
		String whereClause = KottDBOpenHelper.ACTIVITY_ID + " = ?";
		String[] whereArgs = new String [] {activityId};
		String[] columns = {KottDBOpenHelper.IS_COMPLETED};

		Cursor cursor = database.query(KottDBOpenHelper.TABLE_ACTIVITIES, columns, 
				whereClause, whereArgs, null, null, null);

		if (cursor.getCount() > 0){
			while(cursor.moveToNext()){
				cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.IS_COMPLETED));
				close();
			}			
		}
		cursor.close();
		return status;
	}
	
	public List<ClassActivity> getAllActivities(){
		open();
		String[] columns = {KottDBOpenHelper.ACTIVITY_ID, KottDBOpenHelper.DUE_DATE};

		ClassActivity activity = new ClassActivity();
		List<ClassActivity> activities = new ArrayList<ClassActivity>();
		
		Cursor cursor = database.query(KottDBOpenHelper.TABLE_ACTIVITIES, columns, 
				null, null, null, null, null);

		if (cursor.getCount() > 0){
			while(cursor.moveToNext()){
				activity.setActivityId(cursor.getLong(cursor.getColumnIndex(KottDBOpenHelper.ACTIVITY_ID)));
				activity.setDueDate(cursor.getString(cursor.getColumnIndex(KottDBOpenHelper.DUE_DATE)));
				activities.add(activity);	
			}
			close();
		}
		cursor.close();
		return activities;
	}
	
	public Boolean fieldExists(Object query) {
		
		
		
		return null;
	}
}
